package com.infonautics.action;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import com.hibernate.Category;
import com.hibernate.Userinfo;
import com.infonautics.dao.ICategoryDao;
import com.infonautics.dao.imp.ICategoryDaoImp;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.system.dao.ISystemDao;

public class CategoryAction extends ActionSupport implements SessionAware, Preparable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Map<String,Object> session;
	private String pageUrl = "/WEB-INF/category/";
	private String successMsg;
	private String url;
	private String errormessage;
	private Userinfo userinfo;
	private ISystemDao<Category> categoryDao = new ICategoryDaoImp();
	private ICategoryDao catDao = new ICategoryDaoImp();
	private StringBuffer sbtree = new StringBuffer();
	private List<Category> catList = new ArrayList<Category>();
	private Category category;
	private int id;
	
	@Override
	public void prepare() throws Exception {
		if(session.containsKey("userinfo")){
			if(session.get("userinfo")!=null){
				userinfo=(Userinfo)session.get("userinfo");
			}
		}
		
	}
	public String tree() throws Exception{
		sbtree.append("var zNodes = [");
		sbtree=getChildItem(1, sbtree,catDao);
		sbtree.append("];");		
		StringBuffer sbtmp=new StringBuffer();
		sbtmp.append(sbtree.toString().replace("},]", "}]"));
		sbtree=sbtmp;	
		return CategoryAction.SUCCESS;
	}
	private StringBuffer getChildItem(int ParentID,StringBuffer sb,ICategoryDao catdao) throws Exception{
		catList=catdao.doByParentid(ParentID);
		int i=0;
		if(catList!=null&&catList.size()>0){
			for(Category cg:catList){
				i++;
				if(judgepurview(cg.getId(),catdao)){
					sb.append("{");
					sb.append(" id:");
					sb.append(cg.getId());
					sb.append(",");
					sb.append("parentId:");
					sb.append(cg.getParentid());
					sb.append(",");
					sb.append("name:'");
					sb.append(cg.getName());
					sb.append("'");
					if(!cg.getParentid().equals(1)){
						sb.append(",");
						sb.append("url:'");
						sb.append(cg.getMurl());
						sb.append("',");
						sb.append("target:");
						sb.append("'frmright'");
					}
					//判断有没有子项
					if(((BigInteger)catdao.exists("select count(*)from category where parentid="+cg.getId())).intValue()>0){//有	
						sb.append("},");
						getChildItem(cg.getId(),sb,catdao);
					}
					else {//无
						if(i == catList.size()){
							sb.append("},");
						}else{
							sb.append("},");
						}
					}
				}
			}
		}
		return sb;
	}
	private boolean judgepurview(int ClassID,ICategoryDao catdao) throws Exception{
		String sql="select count(*) from assignpurview where roleid="+userinfo.getUserrole().getId().longValue()+" and purviewid="+ClassID;
		if(((BigInteger)catdao.exists(sql)).intValue()>0){
			return true;
		}else{
			return false;
		}
	}
	public String list() throws Exception{
		sbtree.append("var rawdata = [");
		sbtree=getlistItem(1, sbtree,catDao);
		sbtree.append("];");
		StringBuffer sbtmp=new StringBuffer();
		sbtmp.append(sbtree.toString().replace("}{", "},{").replace("},]", "}]"));
		sbtree=sbtmp;		
		return  "input";
	}
	public String addPage() throws Exception{
		sbtree=getTree(0, sbtree,catDao);
		return "input";
	}
	public String add() throws Exception{
		categoryDao.doAdd(category);
		doMsgHandle("添加成功","Category_list.action");
		return CategoryAction.SUCCESS;
	}
	public String modifyPage() throws Exception{
		category = categoryDao.doOne(id);
		return "input";
	}
	private StringBuffer getlistItem(int ParentID,StringBuffer sb,ICategoryDao catDao) throws Exception{
		catList=catDao.doByParentid(ParentID);
		int i=0;
		if(catList!=null&&catList.size()>0){
			for(Category cg:catList){
				i++;
				sb.append("{");
				sb.append("label:'<a href=");
				sb.append("javascript:;");
				sb.append(">");
				sb.append(cg.getName());
				sb.append("</a>',");
				sb.append("link:'");
				sb.append(cg.getMurl());
				sb.append("',");
				sb.append("id:'");
				sb.append(cg.getId());
				sb.append("'");
				
				//判断有没有子项
				if(((BigInteger)catDao.exists("select count(*)from category where parentid="+cg.getId())).intValue()>0){//有
					sb.append(",");
					sb.append("children: [");
					getlistItem(cg.getId(),sb,catDao);
					sb.append("]");
					sb.append("}");
				}
				else {//无
					if(i==catList.size()){
						sb.append("}");
					}else{
						sb.append("},");
					}
					
				}
			}
		}
		return sb;
	}
	private StringBuffer getTree(int ParentID,StringBuffer sb,ICategoryDao catDao) throws Exception{
		catList=catDao.doByParentid(ParentID);
		if(catList!=null&&catList.size()>0){
			for(Category org:catList){
				sb.append("d.add(");
				sb.append(org.getId());
				sb.append(",");
				sb.append(org.getParentid());
				sb.append(",'");
				sb.append(org.getName());
				sb.append("','");
				sb.append("funts(\\'");
				sb.append(org.getId());
				sb.append("\\'");
				sb.append(",");
				sb.append("\\'");
				sb.append(org.getName());
				sb.append("\\')");
				sb.append("','");
				sb.append("','");
				sb.append("frmright");
				sb.append("');\n");
				getTree(org.getId(),sb,catDao);
			}
		}
		return sb;
	}
	//服务方法
	private void doMsgHandle(String successMsg,String url){
		this.successMsg=successMsg;
		this.url=url;
	}
	public Map<String, Object> getSession() {
		return session;
	}
	
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public String getPageUrl() {
		return pageUrl;
	}
	
	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}
	
	public String getSuccessMsg() {
		return successMsg;
	}
	
	public void setSuccessMsg(String successMsg) {
		this.successMsg = successMsg;
	}
	
	public String getUrl() {
		return url;
	}
	
	public void setUrl(String url) {
		this.url = url;
	}
	
	public String getErrormessage() {
		return errormessage;
	}
	
	public void setErrormessage(String errormessage) {
		this.errormessage = errormessage;
	}
	
	public Userinfo getUserinfo() {
		return userinfo;
	}
	
	public void setUserinfo(Userinfo userinfo) {
		this.userinfo = userinfo;
	}
	
	public StringBuffer getSbtree() {
		return sbtree;
	}
	
	public void setSbtree(StringBuffer sbtree) {
		this.sbtree = sbtree;
	}
	
	public List<Category> getCatList() {
		return catList;
	}
	
	public void setCatList(List<Category> catList) {
		this.catList = catList;
	}
	
	public Category getCategory() {
		return category;
	}
	
	public void setCategory(Category category) {
		this.category = category;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}

	
	
	

}
